package com.janal.service.modify;

import com.janal.model.user.User;
import com.janal.model.user.UserDao;
import com.janal.model.user.UserDaoImpl;
import com.janal.util.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class ModifyServiceImpl implements ModifyService{
    private UserDao userDao;

    public ModifyServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public boolean modifyMessage(User user) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = JdbcUtils.getConnection();
            if(userDao.modifyMessage(conn,user)){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,conn);
        }
        return flag;
    }

    @Override
    public boolean modifyPassword(User user) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = JdbcUtils.getConnection();
            if(userDao.modifyPassword(conn,user)){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,conn);
        }
        return flag;
    }

    @Override
    public User getUserMessage(String usercode) {
        Connection conn = null;
        User user = null;
        try {
            conn = JdbcUtils.getConnection();
            user = userDao.getUserMessage(conn,usercode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null,conn);
        }
        return user;
    }
}
